﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using SpotifyAPI;

namespace SpotifyLync.Service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SpotifyRemoteService" in both code and config file together.
    public class SpotifyRemoteService : ISpotifyRemoteService, ISpotifyRemoteLocalInterop
    {

        SpotifyEnhanced api;
        //Spotify legacy;

        #region Base Methods
        
        /// <summary>
        ///   Creates a service instance.
        /// </summary>
        public SpotifyRemoteService()
        {
            InitializeApi();
        }

        /// <summary>
        ///   Initializes the spotify enhanced API
        /// </summary>
        private void InitializeApi()
        {
            try
            {
                api = new SpotifyEnhanced();
                api.Initialize();
                api.IsPlayingChanged += api_IsPlayingChanged;
                api.TrackChanged += api_TrackChanged;
                InitializeSpotify();
            }
            catch
            {
                api = null;
            }
        }

        
        /// <summary>
        ///   Initializes the legacy spotify api.
        /// </summary>
        private void InitializeSpotify()
        {
            //legacy = new Spotify(Spotify.GetOAuth(), "localhost");
            //legacy.Initialize();
        }


        #endregion

        #region Event Handlers
        
        public void api_IsPlayingChanged(object sender, EventArgs e)
        {
            // Not sure if i care to implement this, but it's there.
        }

        public void api_TrackChanged(object sender, EventArgs e)
        {
            // Not sure if i care to implement this, but it's there.
        }

        #endregion

        #region Methods

        public void PlayPause()
        {
            api.PlayPause();
        }

        public void PlayNext()
        {
            api.Next();
        }

        public void PlayPrevious()
        {
            api.Previous();
        }

        public void Mute()
        {
            api.Mute();
        }

        public void VolumeUp()
        {
            api.VolumeUp();
        }

        public void VolumeDown()
        {
            api.VolumeDown();
        }

        /// <summary>
        ///   Gets a flag indicating whether or not the application is currently playing.
        /// </summary>
        public bool IsPlaying() 
        { 
            return api.IsPlaying; 
        }

        /// <summary>
        ///   Gets the track info for the currently playing track.
        /// </summary>
        public string GetTrackInfo()
        {
            if (api.Track != null)
            {
                if ((api.Track.Title != null) && (api.Track.Artist != null))
                {
                    return Newtonsoft.Json.JsonConvert.SerializeObject(api.Track);
                }
                else
                {
                    return null;
                }
            }
            else 
            { 
                return null; 
            }
        }

        
        #endregion
    }
}
